﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace PiratesOfBermuda
{
    /// <summary>
    /// Tinh trang cua tau
    /// </summary>
    public enum ShipState
    {
        Standby, Turning, Moving, Attacking, WhirlIn, WhirlOut, Ended
    };

    /// <summary>
    /// Huong chinh cua con tau
    /// </summary>
    public enum MainSailDirections { North = 0, East = 4, South = 8, West = 12 };

    public class Ship : MySprite
    {
        #region Fields & Accessors
        //Vi tri truoc cua con tau
        public Point LastPosition;
        //Cac bien phuc vu cho viec Xoay(Turning) va Di chuyen tau(Moving)
        protected bool IsTurningClockwise;
        protected int turnStep = 4;
        protected int currentStep = 1;
        protected int currentDirection = 4;
        protected int newX;
        protected int newY;
        //Hang doi luu tru cac Destination rectangle khi tau di chuyen theo 1 huong
        protected Queue<Rectangle> movingAnimation = new Queue<Rectangle>();
        
        public bool IsActive { get; set; }

        ShipState currentShipState;

        public ShipState CurrentShipState
        {
            get { return currentShipState; }
            set { currentShipState = value; }
        }

        private MainSailDirections mainSailDirection;

        public MainSailDirections MainSailDirection
        {
            get { return mainSailDirection; }
        }

        public override Rectangle Source
        {
            get
            {
                Rectangle temp = frames[currentFrame];

                return new Rectangle(temp.X + temp.Width * currentDirection, temp.Y, temp.Width, temp.Height);
            }
        }
        #endregion

        #region Initializes

        public Ship(Game game, SpriteBatch screenSpriteBatch)
            : base(game, screenSpriteBatch)
        {
            mainSailDirection = MainSailDirections.East;
            currentShipState = ShipState.Standby;
        }

        #endregion

        #region Update & Render
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (currentShipState == ShipState.Turning)
            {
                if (currentStep <= turnStep)
                {
                    if (IsTurningClockwise)
                        currentDirection = (currentDirection + 1) % 16;
                    else
                        if (currentDirection == 0)
                            currentDirection = 15;
                        else
                            currentDirection--;
                    currentStep++;
                }
                else
                {
                    currentShipState = ShipState.Moving;
                    X = newX;
                    Y = newY;
                }
            }
            else if (currentShipState == ShipState.Moving)
            {
                if (movingAnimation.Count == 0)
                {
                    currentShipState = ShipState.Standby;
                    IsActive = false;
                }
            }

        }

        public override void Draw(GameTime gameTime)
        {
            if (currentShipState == ShipState.Moving)
                spriteBatch.Draw(Texture, movingAnimation.Dequeue(), Source, tintColor);
            else if (currentShipState == ShipState.Standby
                || currentShipState == ShipState.Turning
                || currentShipState == ShipState.Attacking)
                base.Draw(gameTime);
        }

        #endregion

        #region Public Method
        /// <summary>
        /// Ham dung de di chuyen tau
        /// Truong hop can xoay tau thi cung se phai tinh toan huong xoay va tao Animation phu hop
        /// </summary>
        /// <param name="_newX">Vi tri X moi</param>
        /// <param name="_newY">Vi tri Y moi</param>
        public void Move(int _newX, int _newY)
        {
            LastPosition = new Point(X, Y);
            int newDirection = -1;
            if (X == _newX)
            {
                if (_newY > Y)
                {
                    if (mainSailDirection != MainSailDirections.South)
                    {
                        newDirection = (int)Enum.Parse(typeof(MainSailDirections), Enum.GetName(typeof(MainSailDirections), MainSailDirections.South), true);
                        turnStep = newDirection - currentDirection;
                        mainSailDirection = MainSailDirections.South;
                    }
                    Rectangle currentDest = Destination;

                    for (int i = 1; i < 21; i++)
                    {
                        movingAnimation.Enqueue(new Rectangle(currentDest.X, currentDest.Y + i * 2, currentDest.Width, currentDest.Height));
                    }

                }
                else
                {
                    if (mainSailDirection != MainSailDirections.North)
                    {
                        newDirection = (int)Enum.Parse(typeof(MainSailDirections), Enum.GetName(typeof(MainSailDirections), MainSailDirections.North), true);
                        turnStep = newDirection - currentDirection;
                        mainSailDirection = MainSailDirections.North;
                    }

                    Rectangle currentDest = Destination;

                    for (int i = 1; i < 21; i++)
                    {
                        movingAnimation.Enqueue(new Rectangle(currentDest.X, currentDest.Y - i * 2, currentDest.Width, currentDest.Height));
                    }

                }
            }
            else if (Y == _newY)
            {
                if (_newX > X)
                {
                    if (mainSailDirection != MainSailDirections.East)
                    {
                        newDirection = (int)Enum.Parse(typeof(MainSailDirections), Enum.GetName(typeof(MainSailDirections), MainSailDirections.East), true);
                        turnStep = newDirection - currentDirection;
                        mainSailDirection = MainSailDirections.East;
                    }

                    Rectangle currentDest = Destination;

                    for (int i = 1; i < 21; i++)
                    {
                        movingAnimation.Enqueue(new Rectangle(currentDest.X + i * 2, currentDest.Y, currentDest.Width, currentDest.Height));
                    }

                }
                else
                {
                    if (mainSailDirection != MainSailDirections.West)
                    {
                        newDirection = (int)Enum.Parse(typeof(MainSailDirections), Enum.GetName(typeof(MainSailDirections), MainSailDirections.West), true);
                        turnStep = newDirection - currentDirection;
                        mainSailDirection = MainSailDirections.West;
                    }

                    Rectangle currentDest = Destination;

                    for (int i = 1; i < 21; i++)
                    {
                        movingAnimation.Enqueue(new Rectangle(currentDest.X - i * 2, currentDest.Y, currentDest.Width, currentDest.Height));
                    }

                }
            }
            //Set dieu kien de xoay va chieu` xoay
            if (turnStep > 0)
            {
                if (turnStep == 12)
                {
                    IsTurningClockwise = false;
                    turnStep = 4;
                }
                else
                    IsTurningClockwise = true;
            }
            else
            {
                if (turnStep == -12)
                {
                    IsTurningClockwise = true;
                    turnStep = 4;
                }
                else
                {
                    IsTurningClockwise = false;
                    turnStep *= -1;
                }
            }
            if (newDirection != -1)
            {
                currentShipState = ShipState.Turning;
                currentStep = 1;

            }
            else
            {
                currentShipState = ShipState.Moving;
                X = _newX;
                Y = _newY;
            }

            newX = _newX;
            newY = _newY;
        }
        #endregion
    }
}
